<template>
  <div class="sku">
    <div class="spec-list">
      <div class="sku-title">包装：</div>
    </div>
    <div class="sku-right">
      <div class="sku-spec" :class="{ red: isSelected(v) }" v-for="v in goods?.skus" :key="v.id" @click="toggleSelection(v)">
        <img class="image" :src="v.imageUrl" alt="" />
        <div class="title">
          <el-tooltip placement="top">
            <template #content> {{ v.skuSpec }} </template>
            <el-button link :class="{ selected: isSelected(v) }">{{ v.skuSpec }}</el-button>
          </el-tooltip>
        </div>
      </div>
    </div>
    <div class="clear"></div>
  </div>
</template>

<script lang="ts" setup>
import { ref } from 'vue';

const props = defineProps({
  goods: {
    type: Object as () => { skus: any[]; specList: any[] },
    required: true
  }
});
console.log(props.goods);
const emit = defineEmits(['change']);
const selectedSkuId = ref<string | null>(null);

const toggleSelection = (sku: any) => {
  selectedSkuId.value = sku.id === selectedSkuId.value ? null : sku.id;
  emit('change', selectedSkuId.value ? sku : null);
};

const isSelected = (sku: any) => {
  return selectedSkuId.value === sku.id;
};
</script>

<style lang="scss" scoped>
.red {
  border: 1px solid #ca151e;
  color: #ca151e;
}
.selected {
  color: #ca151e;
}
.sku {
  padding: 10px;
  margin-top: 10px;
  display: flex;
  align-items: center;

  .spec-list {
    display: flex;

    .sku-title {
      width: 100px;
      color: #666;
      line-height: 25px;
      margin-right: 15px;
      display: inline-block;

      &:only-of-type::after {
        content: ' ';
      }
    }
  }

  .sku-right {
    margin-left: -35px;
    display: flex;
    flex-wrap: wrap;

    .sku-spec {
      max-width: 300px;
      margin: 5px;
      border: 1px solid #ccc;
      border-radius: 5px;
      display: flex;
      align-items: center;
      justify-content: center;
      height: 40px;
      overflow: hidden;
      white-space: nowrap;
      text-overflow: ellipsis;

      &.red {
        // 使用 &.red 表示同时拥有 sku-spec 和 red 类的元素
        border: 1px solid #ca151e;
        //color: #ca151e;
      }

      .image {
        padding: 5px;
        width: 30px;
        height: 30px;
      }

      .title {
        max-width: 250px;
        padding: 5px;
        font-size: 16px;
        color: #666;
      }
    }

    // 移除或注释掉以下行，因为它只在悬停时设置样式
    // .sku-spec:hover {
    //   border: 1px solid red;
    // }
  }
}
</style>
